/*!
 * \file t__split_eq_string.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


typedef struct st_test {
    char *p_data;
    char *p_value;
} test_st;

static void __my_list_node_free(Frlist_node *node)
{
    if(((test_st *)node->data)->p_data)
        frfree(((test_st *)node->data)->p_data);

    frfree(((test_st *)node->data)->p_value);
    frfree((test_st *)node->data);
    return;
}

static void __case1(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node_add = NULL;
    int i_ret = 0;
    unsigned int ui_key_len = 0;
    test_st *p_node = NULL;

    //建立struct并对结构体中的内容赋值
    p_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_node == NULL)
        return;

    p_node->p_data = (char *)frmalloc(strlen("test") + 1);
    if(p_node->p_data == NULL)
    {
        frfree(p_node);
        return;
    }
    snprintf(p_node->p_data,strlen("test") + 1,"%s","test");

    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    if(p_node->p_value == NULL)
    {
        frfree(p_node->p_data);
        frfree(p_node);
        return;
    }
    snprintf(p_node->p_value,strlen("test1") + 1,"%s","test1");

    //创建节点
    p_st_node_add = frlist_node_create(p_node);
    if(p_st_node_add == NULL)
    {
        frfree(p_node->p_data);
        frfree(p_node->p_value);
        frfree(p_node);
    }

    p_hash_hdr = NULL;

    //将节点加入hash表
    ui_key_len = strlen(p_node->p_data);
    i_ret = frhash2_addStr(p_hash_hdr, p_node->p_data, ui_key_len, p_st_node_add);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    __my_list_node_free(p_st_node_add);
    frfree(p_st_node_add);

}

static void __case2(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node_add = NULL;
    int i_ret = 0;
    unsigned int ui_key_len = 0;
    test_st *p_node = NULL;

    //建立struct并对结构体中的内容赋值
    p_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_node == NULL)
        return;

    p_node->p_data = (char *)frmalloc(strlen("test") + 1);
    if(p_node->p_data == NULL)
    {
        frfree(p_node);
        return;
    }
    snprintf(p_node->p_data,strlen("test") + 1,"%s","test");

    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    if(p_node->p_value == NULL)
    {
        frfree(p_node->p_data);
        frfree(p_node);
        return;
    }
    snprintf(p_node->p_value,strlen("test1") + 1,"%s","test1");

    //创建节点
    p_st_node_add = frlist_node_create(p_node);
    if(p_st_node_add == NULL)
    {
        frfree(p_node->p_data);
        frfree(p_node->p_value);
        frfree(p_node);
    }
    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    p_hash_hdr->p_list_array = NULL;


    //将节点加入hash表
    ui_key_len = strlen(p_node->p_data);
    i_ret = frhash2_addStr(p_hash_hdr,p_node->p_data,ui_key_len,p_st_node_add);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    __my_list_node_free(p_st_node_add);
    frfree(p_st_node_add);
    frfree(p_hash_hdr);

}

static void __case3(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node_add = NULL;
    unsigned long ul_hash_size = 5;
    int i_ret = 0;
    unsigned int ui_key_len = 0;
    test_st *p_node = NULL;

    //建立struct并对结构体中的内容赋值
    p_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_node == NULL)
        return;

    p_node->p_data = (char *)frmalloc(strlen("test") + 1);
    if(p_node->p_data == NULL)
    {
        frfree(p_node);
        return;
    }
    snprintf(p_node->p_data,strlen("test") + 1,"%s","test");

    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    if(p_node->p_value == NULL)
    {
        frfree(p_node->p_data);
        frfree(p_node);
        return;
    }
    snprintf(p_node->p_value,strlen("test1") + 1,"%s","test1");

    //创建节点  使节点为NULL。测试当节点为NULL时的情况
    p_st_node_add = NULL;

    //创建hash表
    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    frhash2_create(p_hash_hdr,ul_hash_size);

    //把节点加入hash表
    ui_key_len = strlen(p_node->p_data);
    i_ret = frhash2_addStr(p_hash_hdr,p_node->p_data,ui_key_len,p_st_node_add);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    frfree(p_node->p_data);
    frfree(p_node->p_value);
    frfree(p_node);
    frhash2_free2(p_hash_hdr,__my_list_node_free);

}

static void __case4(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node_add = NULL;
    unsigned long ul_hash_size = 5;
    int i_ret = 0;
    unsigned int ui_key_len = 0;
    test_st *p_node = NULL;

    //建立struct并对结构体中的内容赋值
    p_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_node == NULL)
        return;

    p_node->p_data = (char *)frmalloc(strlen("test") + 1);
    if(p_node->p_data == NULL)
    {
        frfree(p_node);
        return;
    }
    snprintf(p_node->p_data,strlen("test") + 1,"%s","test");

    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    if(p_node->p_value == NULL)
    {
        frfree(p_node->p_data);
        frfree(p_node);
        return;
    }
    snprintf(p_node->p_value,strlen("test1") + 1,"%s","test1");

    //创建节点
    p_st_node_add = frlist_node_create(p_node);
    if(p_st_node_add == NULL)
    {
        frfree(p_node->p_data);
        frfree(p_node->p_value);
        frfree(p_node);
    }

    //创建hash表
    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    frhash2_create(p_hash_hdr,ul_hash_size);

    //把节点加入hash表
    ui_key_len = strlen(p_node->p_data);
    i_ret = frhash2_addStr(p_hash_hdr,p_node->p_data,ui_key_len,p_st_node_add);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    //根据p_node->p_data查找插入的节点
    p_st_node_add = frhash2_getStr(p_hash_hdr,p_node->p_data,(unsigned int)strlen(p_node->p_data) );
    CU_ASSERT_STRING_EQUAL(((test_st *)p_st_node_add->data)->p_value,"test1");

    frhash2_free2(p_hash_hdr,__my_list_node_free);

}

static void __case5(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node_add = NULL;
    unsigned long ul_hash_size = 5;
    int i_ret = 0;
    unsigned int ui_key_len = 0;
    test_st *p_node = NULL;

    //建立struct并对结构体中的内容赋值
    p_node = (test_st *)frmalloc(sizeof(test_st));
    if(p_node == NULL)
        return;

    p_node->p_data = NULL;

    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    if(p_node->p_value == NULL)
    {
        frfree(p_node);
        return;
    }
    snprintf(p_node->p_value,strlen("test1") + 1,"%s","test1");

    //创建节点
    p_st_node_add = frlist_node_create(p_node);
    if(p_st_node_add == NULL)
    {
        frfree(p_node->p_value);
        frfree(p_node);
    }

    //创建hash表
    p_hash_hdr = frhash2_create2(ul_hash_size);

    //将节点加入hash表
    i_ret = frhash2_addStr(p_hash_hdr,p_node->p_data,ui_key_len,p_st_node_add);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    __my_list_node_free(p_st_node_add);
    frfree(p_st_node_add);
    frhash2_free2(p_hash_hdr,__my_list_node_free);

}
int add_suite_frhash2_addStr(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frhash2 frhash2_addStr()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);

    return FR_SUCC;
}
